package com.party.admin.biz.serviceguide;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.serviceguide.ServiceGuideType;
import com.party.core.service.serviceguide.IServiceGuideRelationService;
import com.party.core.service.serviceguide.IServiceGuideTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: party
 * @description: 服务指南业务
 * @author: Hua Qunhao
 * @create: 2019-04-17 14:02
 **/
@Service("ServiceGuideBizService")
public class ServiceGuideBizService {
    @Autowired
    private IServiceGuideTypeService serviceGuideTypeService;
    @Autowired
    private IServiceGuideRelationService serviceGuideRelationService;


    public List<ServiceGuideType> getServiceGuideTypeAll() {
        return serviceGuideTypeService.list(new ServiceGuideType());
    }

    public void updateGoodsNum(String sign, String sgId, String newCateIdStr) {

        List<String> ids_new = Lists.newArrayList();

        if (StringUtils.isNotEmpty(newCateIdStr)) {
            ids_new.addAll(Arrays.asList(newCateIdStr.split(",")));
        }
        List<ServiceGuideType> serviceGuideTypes = serviceGuideTypeService.querySGRListbysgId(sgId);
        List<String> ids_old = LangUtils.transform(serviceGuideTypes, new Function<ServiceGuideType, String>() {
            @Override
            public String apply(ServiceGuideType input) {
                return input.getId();
            }
        });

        if ("update".equalsIgnoreCase(sign)) {


            HashMap<String, String> hashMap = Maps.newHashMap();

            for (String str : ids_new) {
                hashMap.put(str, "");
            }
            for (String str : ids_old) {
                hashMap.put(str, "");
            }

            for (Map.Entry<String, String> entry : hashMap.entrySet()) {

                String key = entry.getKey();
                ServiceGuideType serviceGuideType = serviceGuideTypeService.get(key);

                Integer goodsNum = serviceGuideType.getGoodsNum();
                //旧的包含,新的不包含------>删除
                if (ids_old.contains(key) && !ids_new.contains(key)) {
                    serviceGuideType.setGoodsNum(goodsNum - 1);
                    serviceGuideTypeService.update(serviceGuideType);
                }
                //新的包含,旧的不包含------>新增
                if (ids_new.contains(key) && !ids_old.contains(key)) {
                    serviceGuideType.setGoodsNum(goodsNum + 1);
                    serviceGuideTypeService.update(serviceGuideType);
                }
            }
        }

        if ("delete".equalsIgnoreCase(sign)) {
            for (String categoryId : ids_old) {
                ServiceGuideType serviceGuideType = serviceGuideTypeService.get(categoryId);
                serviceGuideType.setGoodsNum(serviceGuideType.getGoodsNum() - 1);
                serviceGuideTypeService.update(serviceGuideType);
            }
        }
    }
}
